package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.VoiceService;
import com.tanhua.server.vo.VoiceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("peachblossom")
public class VoiceController {

    @Autowired
    private VoiceService voiceService;

    /**
     * 发布语音
     * @param soundFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveVoice(@RequestParam(value = "soundFile") MultipartFile soundFile){

        try {
            Boolean bool = voiceService.saveVoice(soundFile);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 接收语音
     * @return
     */
    @GetMapping
    public ResponseEntity<VoiceVo> getVoice(){
        try {
            VoiceVo voiceVo = voiceService.getVoice();
            if (ObjectUtil.isNotEmpty(voiceVo)){
                return ResponseEntity.ok(voiceVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
